package com.hhm.ann;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 切面--添加日志
 */
@Aspect
@Component
public class MyAop {
    //注入HttpServletRequest，用于获取ip地址
    @Autowired
    HttpServletRequest request;

    //--- 如果只写@Around(value = "@annotation(com.hhm.ann.MyLog)")，则参数只能写一个
    //用于获取自定义注解类中value值
    @Pointcut(value = "@annotation(com.hhm.ann.MyLog)")
    public void pointCut(){

    }
    //环绕通知
    @Around(value = "pointCut()&&@annotation(myLog)")
    public Object log(ProceedingJoinPoint pjp, MyLog myLog) throws Throwable {
        //1、获取数据
        //操作的ip
        String ip = request.getRemoteAddr();
        //value值
        String value = myLog.value();
        //获取参数
        Object[] args = pjp.getArgs();
        String str="";
        for (Object arg : args) {
            str+=""+arg;//拼接参数
        }
        //2、存入日志
        System.out.println("ip---"+ip);
        System.out.println("value---"+value);
        System.out.println("str---"+str);
        return pjp.proceed();
    }
}
